//
//  SuperUIScrollView.m
//  极致荟
//
//  Created by hetao on 14-7-11.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "SuperUIScrollView.h"

@implementation SuperUIScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}
//启用刷新加载功能
- (void)hudTypeEnd
{
    hudheide=NO;
    [self setstlable];
    [self hiddenHUD];
    [self setDelegate:self];
}

//隐藏刷新状态提示
- (void)heiderHud
{
    NSLog(@"++%f",self.contentSize.height);
    if(self.contentSize.height>self.frame.size.height+20)
    {
        [UIView animateWithDuration:0.3 animations:^{
            [self setContentSize:CGSizeMake(0, self.contentSize.height-hud.frame.size.height-tslable.frame.size.height)];
            [self setstlable];
            [self hiddenHUD];
        }];

    }
    [self setstlable];
    [self hiddenHUD];
}

///是否开启点击手势，用于键盘回收
- (void)TapGestureEnend:(BOOL) end
{
    if(!tap)
    {
        tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapcilek:)];
        [self addGestureRecognizer:tap];
        
    }
    [tap setEnabled:end];
}

//显示HUD
- (void)showHUD
{
    if(!hud)
    {
        hud=[[UIActivityIndicatorView alloc]init];
        hud.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
        [self addSubview:hud];
    }
    hudheide=NO;
    [hud setFrame:CGRectMake(self.frame.size.width/2-15, self.contentSize.height+20, 30, 30)];
    [hud startAnimating];
    [hud setHidden:NO];
   
}

//隐藏HUD
- (void)hiddenHUD
{
    if(!hud)
    {
        [self showHUD];
    }
    hudheide=YES;
    [hud stopAnimating];
    [hud setHidden:YES];
}

///设置提示lable
- (void)setstlable
{
    if(!tslable)
    {
        tslable=[[UILabel alloc]init];
        [tslable setText:@"继续上拉加载更多...."];
        [tslable setTextColor:[UIColor grayColor]];
        [tslable setFont:[UIFont systemFontOfSize:14.0]];
        tslable.textAlignment=UITextAlignmentCenter;
        [self addSubview:tslable];
    }
    [tslable setFrame:CGRectMake(0, self.contentSize.height, self.frame.size.width, 15)];
}
- (void)tapcilek:(UITapGestureRecognizer *)tap
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

#pragma mark------代理--------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.contentOffset.y+self.frame.size.height >scrollView.contentSize.height+hud.frame.size.height+tslable.frame.size.height-10 && hudheide)
    {
        [tslable setText:@"松开加载更多...."];
    }else
    {
        if(hudheide)
        {
            [tslable setText:@"继续上拉加载更多...."];
        }
        
    }


}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
   
    if(scrollView.contentOffset.y+self.frame.size.height >scrollView.contentSize.height+hud.frame.size.height+tslable.frame.size.height-10 && hudheide)
    {
        [tslable setText:@"正在加载数据...."];
        [self showHUD];
       [UIView animateWithDuration:0.3 animations:^{
              [scrollView setContentSize:CGSizeMake(0, scrollView.contentSize.height+hud.frame.size.height+tslable.frame.size.height)];
        }];
        //调用代理
        if(self.superDelegate)
        {
            [self.superDelegate bottmOffesAddData:(SuperUIScrollView *)scrollView];
        }
        
    
    }else
    {
        if(hudheide)
        {
            [tslable setText:@"继续上拉加载更多...."];
        }

    }
}


@end
